﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using XNAGuiLib.Core.Utilities;

namespace XNAGuiLib.Core.Components
{
    public class RGBSlider : GIComponent
    {

        #region ColorSlider Protected Field - _rcm
        ResourceContentManager _rcm;
        #endregion



        protected Label Label_Red
        { get { return (Label)this._gics[0]; } }

        protected Label Label_Green
        { get { return (Label)this._gics[1]; } }

        protected Label Label_Blue
        { get { return (Label)this._gics[2]; } }

        protected Slider Slider_Red
        { get { return (Slider)this._gics[3]; } }

        protected Slider Slider_Green
        { get { return (Slider)this._gics[4]; } }

        protected Slider Slider_Blue
        { get { return (Slider)this._gics[5]; } }

        protected Colorground Colorground
        { get { return (Colorground)this._gics[6]; } }


        public byte R
        { get { return (byte)Slider_Red.Value; } }

        public byte G
        { get { return (byte)Slider_Green.Value; } }

        public byte B
        { get { return (byte)Slider_Blue.Value; } }

        /* Component mapping for the Window Element
         * =======================================
         * [0]  Label_Red 
         * [1]  Label_Green
         * [2]  Label_Blue
         * [3]  Slider_Red
         * [4]  Slider_Green
         * [5]  Slider_Blue
         * [6]  Colorground
         * 
         * Action mapping for the Window Element
         * =======================================
         * [0]  
         */
        /// <summary>
        /// creates a new ColorSlider based on the resources from the ContentManager
        /// </summary>
        /// <param name="game">the game reference</param>
        /// <param name="rcm">content manager to load resources from</param>
        /// <param name="width">the width of the color slider (>=100 for proper view) </param>
        public RGBSlider(Game game, ResourceContentManager rcm,  int width)
            : base(game)
        {
            this._rcm = rcm;


            Label l_red = new Label(game, rcm, "", 40, 12);
            l_red.Font = rcm.Load<SpriteFont>("font_colorslider");
            l_red.TextValue = "R: ";
            l_red.RelativePosition = new Vector2(1, 0);
            l_red.BackgroundOpaque = 0;
            this.Add(l_red);

            Label l_green = new Label(game, rcm, "G: 255", 40, 12);
            l_green.Font = rcm.Load<SpriteFont>("font_colorslider");
            l_green.TextValue = "G: ";
            l_green.RelativePosition = new Vector2(1, 12);
            l_green.BackgroundOpaque = 0;
            this.Add(l_green);

            Label l_blue = new Label(game, rcm, "B: 255", 40, 12);
            l_blue.Font = rcm.Load<SpriteFont>("font_colorslider");
            l_blue.TextValue = "B: ";
            l_blue.RelativePosition = new Vector2(1, 24);
            l_blue.BackgroundOpaque = 0;
            this.Add(l_blue);

            int length = width - 70;
            float stepValue = 255.0f / (float)length;

            Slider slider_r = new Slider(game, rcm, (int)length, Enums.SliderOrientation.Horizontal);
            slider_r.RelativePosition = new Vector2(36, 1);
            slider_r.StepValue = stepValue;
            this.Add(slider_r);

            Slider slider_g = new Slider(game, rcm, length, Enums.SliderOrientation.Horizontal);
            slider_g.RelativePosition = new Vector2(36, 14);
            slider_g.StepValue = stepValue;
            this.Add(slider_g);

            Slider slider_b = new Slider(game, rcm, length, Enums.SliderOrientation.Horizontal);
            slider_b.RelativePosition = new Vector2(36, 27);
            slider_b.StepValue = stepValue;
            this.Add(slider_b);

            Colorground cg = new Colorground(game, 22, 36);
            cg.RelativePosition = new Vector2(l_green.Width + slider_g.Width + 8, 1);
            cg.Color = Color.White;
            this.Add(cg);
             
        }

        public override void Update(GameTime gameTime)
        {
            this.Colorground.Color = ColorBlend.ModulateRGB(this.Colorground.Color, (byte)this.Slider_Red.Value, (byte)this.Slider_Green.Value, (byte)this.Slider_Blue.Value);
            this.Label_Red.TextValue = "R: " + (byte)this.Slider_Red.Value;
            this.Label_Green.TextValue = "G: " + (byte)this.Slider_Green.Value;
            this.Label_Blue.TextValue = "B: " + (byte)this.Slider_Blue.Value;
            base.Update(gameTime);
        }
    }
}
